package top.itaso.app.ui.camera;

import android.Manifest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;

import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.RequestCallback;

import java.util.List;

import top.itaso.app.databinding.FragmentCameraBinding;
import top.itaso.lib.alog.ALog;

public class CameraFragment extends Fragment {
    private static final String TAG = "CameraFragment";
    private CameraViewModel cameraViewModel;
    private FragmentCameraBinding binding;

    private TakePhotoSurfaceView mCameraPreview;

    private Button mTakePhoto;

    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        cameraViewModel =
                new ViewModelProvider(this).get(CameraViewModel.class);

        binding = FragmentCameraBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        mCameraPreview = binding.cameraPreview;
        mTakePhoto = binding.takePhoto;
        mTakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTakePhoto.setEnabled(false);
                mCameraPreview.takePhoto("take_photo", new TakePhotoSurfaceView.ITakePhotoCallback() {
                    @Override
                    public void onTakePhotoOver(String filePath) {
                        ALog.dTag(TAG, "照片存储路径==%s", filePath);
                        mTakePhoto.setEnabled(true);
                        binding.takePhotoResult.setText("已保存至：" + filePath);
                    }

                    @Override
                    public void onTakePhotoErr(int errCode, String errmsg) {
                        mTakePhoto.setEnabled(true);
                        binding.takePhotoResult.setText(errmsg);
                    }
                });
            }
        });
        return root;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        PermissionX.init(this)
                .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (!allGranted) {
                            Toast.makeText(requireContext(), "请到设置中打开相关权限，确保功能可用", Toast.LENGTH_LONG).show();
                            if (callback != null) {
                                callback.handleOnBackPressed();
                            }
                        }
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    OnBackPressedCallback callback = new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            // 返回监听 做你想做的
            ALog.v(">>>>>>......");
            NavHostFragment.findNavController(CameraFragment.this).popBackStack();
        }
    };
}